﻿using System.Collections.Generic;
using QFramework;
using TodoPro.Utility;

namespace TodoPro
{
    public enum TodoFilter
    {
        /// <summary>
        /// 获取全部 Todo
        /// </summary>
        All,
        
        /// <summary>
        /// 获取已激活 Todo
        /// </summary>
        Active,
        
        /// <summary>
        /// 获取已完成 Todo
        /// </summary>
        Completed
    }

    public interface ITodoModel : IModel
    {
        TodoFilter Filter { get; set; }

        void Save();

        void Load();
        
        void Add(Todo todo);
        void Remove(Todo todo);
        void Remove(string id);
        void Update(Todo todo);

        List<Todo> GetAll();
    }
    
    /// <summary>
    /// 
    /// </summary>
    public class TodoModel : ITodoModel
    {
        List<Todo> mTodos = new List<Todo>()
        {
            new Todo() {Id = "1",Content = "去买菜"},
            new Todo() {Id = "2",Content = "去学习"},
            new Todo() {Id = "3",Content = "去学小班"},
            new Todo() {Id = "4",Content = "去学框架搭建"},
            new Todo() {Id = "5",Content = "去写框架"},
            new Todo() {Id = "6",Content = "去写 Shader"},
        };


        public TodoFilter Filter { get; set; }

        /// <summary>
        /// 保存
        /// </summary>
        public void Save()
        {
            var storage = TodoProConfig.GetUtility<IStorage>();

            storage.SaveObject("TODOS", mTodos);

            storage.SaveInt("FILTER", (int) Filter);
        }

        /// <summary>
        /// 加载
        /// </summary>
        public void Load()
        {
            var storage = TodoProConfig.GetUtility<IStorage>();

            mTodos = storage.LoadObject<List<Todo>>("TODOS", mTodos);

            Filter = (TodoFilter) storage.LoadInt("FILTER", 0);
        }

        public void Add(Todo todo)
        {
            mTodos.Add(todo);
        }

        public void Remove(Todo todo)
        {
            mTodos.Remove(todo);
        }

        public void Remove(string id)
        {
            mTodos.RemoveAll(todo => todo.Id == id);
        }

        public void Update(Todo todo)
        {
            // 因为 Todo 是引用类型所以不用做什么
        }

        public List<Todo> GetAll()
        {
            return mTodos;
        }
    }
}